﻿namespace Chain.Common
{
    using System;
    using System.Globalization;

    public class TimeParser
    {
        public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            string str = null;
            try
            {
                TimeSpan span = (TimeSpan) (DateTime2 - DateTime1);
                if (span.Days >= 1)
                {
                    return (DateTime1.Month.ToString() + "月" + DateTime1.Day.ToString() + "日");
                }
                if (span.Hours > 1)
                {
                    return (span.Hours.ToString() + "小时前");
                }
                str = span.Minutes.ToString() + "分钟前";
            }
            catch
            {
            }
            return str;
        }

        public static int[] GetLunisolarDate(DateTime date)
        {
            ChineseLunisolarCalendar calendar = new ChineseLunisolarCalendar();
            int[] numArray = new int[3];
            int year = calendar.GetYear(date);
            int month = calendar.GetMonth(date);
            int dayOfMonth = calendar.GetDayOfMonth(date);
            int leapMonth = calendar.GetLeapMonth(year);
            if (leapMonth > 0)
            {
                if (month == leapMonth)
                {
                    numArray[0] = month - 1;
                    numArray[1] = dayOfMonth;
                    numArray[2] = year;
                    return numArray;
                }
                if (month > leapMonth)
                {
                    numArray[0] = month - 1;
                    numArray[1] = dayOfMonth;
                    numArray[2] = year;
                    return numArray;
                }
                numArray[0] = month;
                numArray[1] = dayOfMonth;
                numArray[2] = year;
                return numArray;
            }
            numArray[0] = month;
            numArray[1] = dayOfMonth;
            numArray[2] = year;
            return numArray;
        }

        public static int GetMonthLastDate(int year, int month)
        {
            DateTime time = new DateTime(year, month, new GregorianCalendar().GetDaysInMonth(year, month));
            return time.Day;
        }

        public static int SecondToMinute(int Second)
        {
            decimal d = Second / 60M;
            return Convert.ToInt32(Math.Ceiling(d));
        }
    }
}

